package com.kushe.util.core;

import com.kushe.config.Config;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
 * Created by WhiteBlue on 15/4/25.
 */
public class Controller {
    ModelAndView mv;

    public Controller() {
        mv = new ModelAndView();
    }

    public ModelAndView render(String fileName) {
        mv.setViewName(fileName);
        return mv;
    }

    public ModelAndView renderError(String Error) {
        mv.setViewName(Config.ERROR_PAGE);
        mv.addObject("msg", Error);
        return mv;
    }

    public Object renderError(int error, HttpServletResponse response) {
        try {
            response.sendError(error);
            return null;
        } catch (IOException e) {
            return null;
        }
    }

    public Object renderText(HttpServletResponse response, String text) {
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            outputStream.print(text);
            outputStream.flush();
            outputStream.close();
            return null;
        } catch (Exception e) {
            return Config.ERROR_PAGE;
        }
    }

    public Object renderJson(HttpServletResponse response, Object target) {
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            JSONArray json = JSONArray.fromObject(target);
            outputStream.print(json.toString());
            outputStream.flush();
            outputStream.close();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String uploadImg(MultipartFile file, HttpServletRequest request){
        //上传图片
        String path = request.getSession().getServletContext().getRealPath("upload");
        //String fileName = file.getOriginalFilename();
        String filename = UUID.randomUUID().toString();
        filename = filename + ".jpg";
        System.out.println(path);
        File targetFile = new File(path, filename);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }
        //保存
        try {
            file.transferTo(targetFile);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return filename;
    }

    public Object renderJsonObject(HttpServletResponse response, Object target) {
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            JSONObject json = JSONObject.fromObject(target);
            outputStream.print(json.toString());
            outputStream.flush();
            outputStream.close();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public ModelAndView renderError(String Error, Set<String> errors) {
        mv.setViewName(Config.ERROR_PAGE);
        mv.addObject("msg", Error);
        mv.addObject("errors", errors);
        return mv;
    }

    public String getPara(HttpServletRequest request, String name) {
        return StringEscapeUtils.escapeHtml4(request.getParameter(name));
    }

    public String getPara(HttpServletRequest request, String name, String defaultValue) {
        String back = StringEscapeUtils.escapeHtml4(request.getParameter(name));
        if ((back == null) || (back.equals("")))
            return defaultValue;
        else
            return back;
    }

    public void clearAttr(HttpServletRequest request) {
        this.mv.clear();
        String msg = (String) request.getSession().getAttribute("msg");
        if (msg != null) {
            this.setAttr("msg", msg);
            request.getSession().removeAttribute("msg");
        }
    }

    public Object getAttr(String key) {
        return mv.getModelMap().get(key);
    }

    public String redirect(String url) {
        return "redirect:" + url;
    }

    public String forward(String url) {
        return "forward:" + url;
    }

    public void setAttr(String name, Object obj) {
        mv.addObject(name, obj);
    }

    public void setAttr(Map<String, ?> map) {
        mv.addAllObjects(map);
    }


    public JSONArray toJson(Map map) {
        return JSONArray.fromObject(map);
    }
}
